import { api } from '@/scripts/api'

const install = (Vue) => {
  const plugins = {
    $api: api
  }
  Object.assign(Vue.prototype, plugins)
  Vue.filter('tab', function (value) {
    if (!value) return ''
    value = value.toString()
    if (value == 'ask') {
      return '问答'
    } else if (value == 'share') {
      return '分享'
    } else if (value == 'job') {
      return '招聘'
    } else if (value == 'good') {
      return '精华'
    } else if (value == 'dev') {
      return '客户端测试'
    }
  })

  Vue.filter('date', function (date) {
    let dateT = date.substr(0, date.indexOf('T')).split('-'),
      dateZ = date.substr(date.indexOf('T') + 1, 8).split(':')

    let now = new Date()
    let nowYear = now.getFullYear()
    let nowMonth = now.getMonth() + 1
    let nowDay = now.getDate()
    let nowHour = now.getHours()
    let nowMin = now.getMinutes()
    if (nowYear - dateT[0] > 0) {
      return `${nowYear - dateT[0]}年前`
    } else {
      if (nowMonth - dateT[1] > 0) {
        return `${nowMonth - dateT[1]}月前`
      } else {
        if (nowDay - dateT[2] > 0) {
          return `${nowDay - dateT[2]}天前`
        } else {
          if (nowHour - dateZ[0] > 0) {
            return `${nowHour - dateZ[0]}小时前`
          } else {
            if (nowMin - dateZ[1] > 0) {
              return `${nowMin - dateZ[1]}分前`
            } else {
              return `刚刚`
            }
          }
        }
      }
    }
    // if end
  })
}

export default { install }